package com.zyg.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.user.dao.UserMapper;
import com.zyg.user.enttiy.User;
import com.zyg.user.service.UserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 作者 : LMY
     * 功能 : 发送验证码
     * 1.生成一个6位的随机数
     *
     * 时间 : 2021/11/9 11:22
     */
    @Override
    public void sendCode(String phone) {
        //1. 生成随机数
        int min = 100000;   //六位数的最小值
        int max = 999999;   //六位数的最大值
        Random random = new Random();
        int code = random.nextInt(max-min);  //得到一个[0,max-min)范围的随机数
        if(code < min){
            code = code + min ;
        }
        System.out.println("生成的验证码为 = " + code);
        //2. 手机号为key,验证码为value存储到redis
        redisTemplate.opsForValue().set(phone,code+ "",10, TimeUnit.HOURS);

        Map<String,String> map = new HashMap<>();
        map.put("phone",phone);
        map.put("signName","泽易购");
        map.put("templateCode","SMS_195720463");
        //{"code":"xxxx"}
        Map<String,String> small = new HashMap<>();
        small.put("code", ""+code);
        map.put("templateParam", JSON.toJSONString(small));
        rabbitTemplate.convertAndSend("sms",map);
    }
}
